# -*- coding: utf-8 -*-
"""
Created on Fri Mar 26 07:48:05 2021

@author: 雁痕
"""

from datetime import datetime
from uuid import UUID,uuid4

from basic import Base

class Transaction(Base):
    def __init__(self,
                 sender:str,receiver:str,
                 amount:int,t:float=None,
                 prev_hash:str = None,
                 transaction_id:str = None):
        assert UUID(sender,version=4)
        assert UUID(receiver,version=4)
        assert transaction_id is None or UUID(transaction_id,version=4)
        self.id = transaction_id if transaction_id is not None else str(uuid4())
        self.sender = sender
        self.receiver = receiver
        self.amount = amount
        self.t = t if t is not None else datetime.now().timestamp()
        self.prev_hash = prev_hash
        
    def __eq__(self, other):
        return \
            self.id == other.id and \
            self.sender == other.sender and \
            self.receiver == other.receiver and \
            self.amount == other.amount
            
    def __hash__(self):
        return hash((self.id, self.sender, self.receiver,      self.amount))
